# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
#                                                                              #
# TITLE      :  Breaking Silence: How Intimate Partner Violence                #
#               And Reporting Shape Later Life Outcomes                        #
# AUTHOR     : Harrison Chang, Shiau-Fang Chao,                                #
#              Kuan-Ming Chen, Ming-Jen Lin                                    #
# CODE EDITOR: Harrison Chang                                                  #
# PURPOSE    : This .R provides necessary functions                            #
#                                                                              #
#                                                                              #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

graph <- function(dt){
  
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.1", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  
  dt[, c("first","stratify_group") := tstrsplit(variable,".",fixed=T)]
  dt[, stratify_group              := as.character(stratify_group)]
  dt[, coefficient                 := str_extract(first,'([a-z\\_]+)')]
  dt[, event_time                  := str_extract(first,'([-0-9]+)')]
  dt[, event_time                  := as.numeric(event_time)]
  dt[, conf_upb                    := Estimate + 1.96*`Std. Error`]
  dt[, conf_lwb                    := Estimate - 1.96*`Std. Error`]
  
  dt <- dt[event_time %in% c(left_bound:right_bound)]
  
  figure <- dt[str_sub(variable,1,1)=="t"] %>%
    ggplot()+
    geom_hline(yintercept=baseline, color = 'black')+
    geom_vline(xintercept = -1.5, linetype = "dotted", color = "gray30")+
    geom_line(aes( x = event_time, y = Estimate, color = stratify_group))+
    geom_point(aes( x = event_time, y = Estimate, color = stratify_group))+
    geom_errorbar(
      aes(x=event_time,
          ymin = conf_lwb,
          ymax = conf_upb),
      linetype = "dashed",
      width=0.1,
      color = "black")+
    theme_classic()+
    theme(plot.title = element_text(hjust=0.5),
          plot.subtitle = element_text(hjust=0.5),
          plot.caption = element_text(hjust=0),
          legend.position = "none")+
    scale_y_continuous(name=ylabel, limits=yscale, breaks = ybks)+
    scale_x_continuous(name=xlabel, breaks = xbks)+
    labs(title = main_title,
         subtitle = subtitle)
  return(figure)
}



graph2_color2 <- function(dt){
  
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.1", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.0", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  
  dt[, c("first","stratify_group") := tstrsplit(variable,".",fixed=T)]
  dt[, stratify_group              := as.character(stratify_group)]
  dt[, coefficient                 := str_extract(first,'([a-z\\_]+)')]
  dt[, event_time                  := str_extract(first,'([-0-9]+)')]
  dt[, event_time                  := as.numeric(event_time)]
  dt[, conf_upb                    := Estimate + 1.96*`Std. Error`]
  dt[, conf_lwb                    := Estimate - 1.96*`Std. Error`]
  
  dt <- dt[event_time %in% c(left_bound:right_bound)]
  
  figure <- dt[str_sub(variable,1,1)=="t"] %>%
    ggplot()+
    geom_vline(xintercept = -1.5, linetype = "dotted", color = "gray30")+
    geom_hline(yintercept=baseline, color = 'black')+
    geom_line(aes( x = event_time, y = Estimate, color = stratify_group), show.legend = TRUE)+
    geom_point(aes( x = event_time, y = Estimate, color = stratify_group,
                    shape = stratify_group), size = 2)+
    geom_errorbar(
      aes(x=event_time,
          ymin = conf_lwb,
          ymax = conf_upb,
          color = stratify_group),
      linetype = "dashed",
      width=0.1)+
    theme_classic()+
    scale_colour_manual(values = c("orange", 'blue'))+
    scale_shape_manual(values=c(15,16),
                       name = stratify_title,
                       labels = stratify_group)+ 
    theme(plot.title = element_text(hjust=0.5),
          plot.subtitle = element_text(hjust=0.5),
          plot.caption = element_text(hjust=0),
          legend.position = "bottom")+
    scale_y_continuous(name=ylabel, limits=yscale, breaks = ybks)+
    scale_x_continuous(name=xlabel, breaks = xbks)+
    labs(title = main_title,
         subtitle = subtitle)+
    guides(color = FALSE, 
           shape = guide_legend(override.aes = list(color = c("orange", 'blue'),
                                                    shape = c(15,16))))  
  return(figure)
}


graph_geom <- function(dt){
  
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.1", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.2", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.3", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  
  dt[, c("first","stratify_group") := tstrsplit(variable,".",fixed=T)]
  dt[, stratify_group              := as.character(stratify_group)]
  dt[, coefficient                 := str_extract(first,'([a-z\\_]+)')]
  dt[, event_time                  := str_extract(first,'([-0-9]+)')]
  dt[, event_time                  := as.numeric(event_time)]
  dt[, conf_upb                    := Estimate + 1.96*`Std. Error`]
  dt[, conf_lwb                    := Estimate - 1.96*`Std. Error`]
  
  dt <- dt[event_time %in% c(left_bound:right_bound)]
  
  figure <- dt[str_sub(variable,1,1)=="t"] %>%
    ggplot()+
    geom_vline(xintercept = -1.5, linetype = "dotted", color = "gray30")+
    geom_hline(yintercept=baseline, color = 'black')+
    geom_line(aes( x = event_time, y = Estimate, color = stratify_group), show.legend = TRUE)+
    geom_point(aes( x = event_time, y = Estimate, color = stratify_group,
                    shape = stratify_group), size = 2)+
    geom_errorbar(
      aes(x=event_time,
          ymin = conf_lwb,
          ymax = conf_upb,
          color = stratify_group),
      linetype = "dashed",
      width=0.1)+
    theme_classic()+
    scale_colour_manual(values = c("#F8766D","#00BFC4","#A9A9A9"))+
    scale_shape_manual(values=c(15,16,17),
                       name = stratify_title,
                       labels = stratify_group)+ 
    theme(plot.title = element_text(hjust=0.5),
          plot.subtitle = element_text(hjust=0.5),
          plot.caption = element_text(hjust=0),
          legend.position = "bottom")+
    scale_y_continuous(name=ylabel, limits=yscale, breaks = ybks)+
    scale_x_continuous(name=xlabel, breaks = xbks)+
    labs(title = main_title,
         subtitle = subtitle)+
    guides(color = FALSE, 
           shape = guide_legend(override.aes = list(color = c("#F8766D","#00BFC4","#A9A9A9"),
                                                    shape = c(15,16,17))))  
  return(figure)
}


graph_raw <- function(dt){
  
  dt[, stratify_group := as.character(dv)][, dv := NULL]
  
  dt <- dt[event_time %in% xbks]
  
  figure <- dt %>%
    ggplot()+
    geom_line(aes( x = event_time, y = mean, color = stratify_group), show.legend = TRUE)+
    geom_point(aes( x = event_time, y = mean, color = stratify_group,
                    shape = stratify_group), size = 2)+
    theme_classic()+
    scale_colour_manual(values = c("#F8766D","#00BFC4"))+
    scale_shape_manual(values=c(15,16),
                       name = stratify_title,
                       labels = stratify_group)+ 
    theme(plot.title = element_text(hjust=0.5),
          plot.subtitle = element_text(hjust=0.5),
          plot.caption = element_text(hjust=0),
          legend.position = "bottom")+
    scale_y_continuous(name=ylabel, limits=yscale, breaks = ybks)+
    scale_x_continuous(name=xlabel, breaks = xbks)+
    labs(title = main_title,
         subtitle = subtitle)+
    guides(color = FALSE, 
           shape = guide_legend(override.aes = list(color = c("#F8766D","#00BFC4"),
                                                    shape = c(15,16))))  
  return(figure)
}





prep_compare <- function(dd){
  
  dt <- copy(dd)
  
  dt[, c("first","stratify_group") := tstrsplit(variable,".",fixed=T)]
  dt[, coefficient                 := str_extract(first,'([a-z\\_]+)')]
  dt[, event_time                  := str_extract(first,'([-0-9]+)')]
  dt[, event_time                  := as.numeric(event_time)]
  dt[, conf_upb                    := Estimate + 1.96*`Std. Error`]
  dt[, conf_lwb                    := Estimate - 1.96*`Std. Error`]
  
  dt       <- dt[event_time %in% c(left_bound:right_bound)][order(event_time)]
  dt       <- dt[, .(event_time,att=Estimate,conf_upb,conf_lwb)]
  dt[, reg := 0]
  
  dt       <- add_row(dt, event_time = -2, att = 0, conf_upb = 0, conf_lwb = 0, reg=0)
  
  
  return(dt)
  
}


graph2 <- function(dt){
  
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.1", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.0", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  
  dt[, c("first","stratify_group") := tstrsplit(variable,".",fixed=T)]
  dt[, stratify_group              := as.character(stratify_group)]
  dt[, coefficient                 := str_extract(first,'([a-z\\_]+)')]
  dt[, event_time                  := str_extract(first,'([-0-9]+)')]
  dt[, event_time                  := as.numeric(event_time)]
  dt[, conf_upb                    := Estimate + 1.96*`Std. Error`]
  dt[, conf_lwb                    := Estimate - 1.96*`Std. Error`]
  
  dt <- dt[event_time %in% c(left_bound:right_bound)]
  
  figure <- dt[str_sub(variable,1,1)=="t"] %>%
    ggplot()+
    geom_vline(xintercept = -1.5, linetype = "dotted", color = "gray30")+
    geom_hline(yintercept=baseline, color = 'black')+
    geom_line(aes( x = event_time, y = Estimate, color = stratify_group), show.legend = TRUE)+
    geom_point(aes( x = event_time, y = Estimate, color = stratify_group,
                    shape = stratify_group), size = 2)+
    geom_errorbar(
      aes(x=event_time,
          ymin = conf_lwb,
          ymax = conf_upb,
          color = stratify_group),
      linetype = "dashed",
      width=0.1)+
    theme_classic()+
    scale_colour_manual(values = c("#F8766D","#00BFC4"))+
    scale_shape_manual(values=c(15,16),
                       name = stratify_title,
                       labels = stratify_group)+ 
    theme(plot.title = element_text(hjust=0.5),
          plot.subtitle = element_text(hjust=0.5),
          plot.caption = element_text(hjust=0),
          legend.position = "bottom")+
    scale_y_continuous(name=ylabel, limits=yscale, breaks = ybks)+
    scale_x_continuous(name=xlabel, breaks = xbks)+
    labs(title = main_title,
         subtitle = subtitle)+
    guides(color = FALSE, 
           shape = guide_legend(override.aes = list(color = c("#F8766D","#00BFC4"),
                                                    shape = c(15,16))))  
  return(figure)
}




graph_three_dot <- function(dt){
  
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.1", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.2", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.3", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  
  dt[, c("first","stratify_group") := tstrsplit(variable,".",fixed=T)]
  dt[, stratify_group              := as.character(stratify_group)]
  dt[, coefficient                 := str_extract(first,'([a-z\\_]+)')]
  dt[, event_time                  := str_extract(first,'([-0-9]+)')]
  dt[, event_time                  := as.numeric(event_time)]
  dt[, conf_upb                    := Estimate + 1.96*`Std. Error`]
  dt[, conf_lwb                    := Estimate - 1.96*`Std. Error`]
  
  dt <- dt[event_time %in% c(left_bound:right_bound)]
  
  figure <- dt[str_sub(variable,1,1)=="t"] %>%
    ggplot()+
    geom_vline(xintercept = -1.5, linetype = "dotted", color = "gray30")+
    geom_hline(yintercept=baseline, color = 'black')+
    geom_point(aes( x = event_time, y = Estimate, color = stratify_group,
                    shape = stratify_group),
               size = 2, position = position_dodge(width = 0.5))+
    geom_errorbar(
      aes(x=event_time,
          ymin = conf_lwb,
          ymax = conf_upb,
          color = stratify_group),
      linetype = "dashed",
      width=0.1,position = position_dodge(width = 0.5))+
    theme_classic()+
    scale_colour_manual(values = c("#F8766D", "#00BFC4", "#A9A9A9"))+
    scale_shape_manual(values=c(15,16,17),
                       name = stratify_title,
                       labels = stratify_group)+ 
    theme(plot.title = element_text(hjust=0.5),
          plot.subtitle = element_text(hjust=0.5),
          plot.caption = element_text(hjust=0),
          legend.position = "bottom")+
    scale_y_continuous(name=ylabel, limits=yscale, breaks = ybks)+
    scale_x_continuous(name=xlabel, breaks = xbks)+
    labs(title = main_title,
         subtitle = subtitle)+
    guides(color = FALSE, 
           shape = guide_legend(override.aes = list(color = c("#F8766D", "#00BFC4", "#A9A9A9"),
                                                    shape = c(15,16,17))))  
  return(figure)
}


prep_compare2 <- function(dt){
  
  dt <- copy(dt)
  
  dt[, conf_upb               := att_r + 1.96*`std`]
  dt[, conf_lwb               := att_r - 1.96*`std`]
  dt  <- dt[, .(event_time=r,att=att_r,conf_upb,conf_lwb,reg)]
  
  dt[reg=="csdid", reg := 1]
  dt[reg=="twfe" , reg := 2]
  dt[reg=="sa"   , reg := 3]
  
  return(dt)
  
}


graph_compare <- function(dt){      
  figure <- dt %>% 
    ggplot() + 
    geom_vline(xintercept = -1.5, linetype = "dotted", color = "gray30")+
    geom_point(aes(x = event_time, y = att, color = reg, shape = reg), 
               position = position_dodge(width = 0.3), size = 3) + 
    geom_hline(yintercept=baseline, color = 'black')+
    geom_errorbar(aes(x = event_time, ymin = conf_lwb, ymax = conf_upb, color = reg), 
                  linetype = "dashed", width = 0.1, 
                  position = position_dodge(width = 0.3)) + 
    theme_classic() + 
    scale_shape_manual(values = c(15, 16, 17), 
                       name = stratify_title, 
                       labels = stratify_group) + 
    scale_color_manual(values = c("#F8766D", "#00BFC4", "#A9A9A9"), 
                       name = stratify_title, 
                       labels = stratify_group) + 
    theme(plot.title = element_text(hjust = 0.5), 
          plot.subtitle = element_text(hjust = 0.5), 
          plot.caption = element_text(hjust = 0), 
          legend.position = "bottom") + 
    scale_y_continuous(name = ylabel, limits = yscale, breaks = ybks) + 
    scale_x_continuous(name = xlabel, breaks = xbks) + 
    labs(title = main_title, subtitle = subtitle)
  
  return(figure)
}



graph_csdid <- function(dt){
  
  dt[, stratify_group              := factor(reg)]
  dt[, Estimate                    := att]
  
  dt <- dt[event_time %in% c(left_bound:right_bound)]
  
  figure <- dt %>%
    ggplot()+
    geom_vline(xintercept = -1.5, linetype = "dotted", color = "gray30")+
    geom_hline(yintercept=baseline, color = 'black')+
    geom_line(aes( x = event_time, y = Estimate, color = stratify_group), show.legend = TRUE)+
    geom_point(aes( x = event_time, y = Estimate, color = stratify_group,
                    shape = stratify_group), size = 2)+
    geom_errorbar(
      aes(x=event_time,
          ymin = conf_lwb,
          ymax = conf_upb,
          color = stratify_group),
      linetype = "dashed",
      width=0.1)+
    theme_classic()+
    scale_colour_manual(values = c("#F8766D","#00BFC4"))+
    scale_shape_manual(values=c(15,16),
                       name = stratify_title,
                       labels = stratify_group)+ 
    theme(plot.title = element_text(hjust=0.5),
          plot.subtitle = element_text(hjust=0.5),
          plot.caption = element_text(hjust=0),
          legend.position = "bottom")+
    scale_y_continuous(name=ylabel, limits=yscale, breaks = ybks)+
    scale_x_continuous(name=xlabel, breaks = xbks)+
    labs(title = main_title,
         subtitle = subtitle)+
    guides(color = FALSE, 
           shape = guide_legend(override.aes = list(color = c("#F8766D","#00BFC4"),
                                                    shape = c(15,16)))) 
  return(figure)
}





violence_only <- function(dt) {
  
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.1", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.2", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.3", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  
  dt[, event_time                  := str_extract(variable,'([-0-9]+)')]
  dt[, event_time                  := as.numeric(event_time)]
  dt[, conf_upb                    := Estimate + 1.96*`Std. Error`]
  dt[, conf_lwb                    := Estimate - 1.96*`Std. Error`]
  dt[, reg := str_sub(variable,-1,-1) %>% as.numeric() %>% as.factor()]
  
  dt <- dt[event_time %in% c(left_bound:right_bound)]
  
  
  figure <- dt %>%
    ggplot(aes(x = event_time, group = reg)) +  # set group to reg
    geom_vline(xintercept = -1.5, linetype = "dotted", color = "gray30")+
    geom_point(aes(y = Estimate, color = reg, shape = reg),
               position = position_dodge(width = 0.3), size = 3) +
    geom_hline(yintercept = baseline, color = 'black') +
    geom_errorbar(aes(ymin = conf_lwb, ymax = conf_upb, color = reg),
                  linetype = "dashed", width = 0.1,
                  position = position_dodge(width = 0.3)) +
    theme_classic() +
    scale_shape_manual(values = c(15, 16, 17),
                       name = stratify_title,
                       labels = stratify_group) +
    scale_color_manual(values = c("#F8766D", "#00BFC4", "#A9A9A9"),
                       name = stratify_title,
                       labels = stratify_group) +
    theme(plot.title = element_text(hjust = 0.5),
          plot.subtitle = element_text(hjust = 0.5),
          plot.caption = element_text(hjust = 0),
          legend.position = "bottom") +
    scale_y_continuous(name = ylabel, limits = yscale, breaks = ybks) +
    scale_x_continuous(name = xlabel, breaks = xbks) +
    labs(title = main_title, subtitle = subtitle)
  
  return(figure)
}



prep_compare_b1 <- function(dd){
  
  dt <- copy(dd)
  
  dt[, c("first","stratify_group") := tstrsplit(variable,".",fixed=T)]
  dt[, coefficient                 := str_extract(first,'([a-z\\_]+)')]
  dt[, event_time                  := str_extract(first,'([-0-9]+)')]
  dt[, event_time                  := as.numeric(event_time)]
  dt[, conf_upb                    := Estimate + 1.96*`Std. Error`]
  dt[, conf_lwb                    := Estimate - 1.96*`Std. Error`]
  
  dt       <- dt[event_time %in% c(left_bound:right_bound)][order(event_time)]
  dt       <- dt[, .(event_time,att=Estimate,conf_upb,conf_lwb)]
  dt[, reg := 0]
  
  dt       <- add_row(dt, event_time = -1, att = 0, conf_upb = 0, conf_lwb = 0, reg=0)
  
  
  return(dt)
  
}





graph2_color <- function(dt){
  
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.1", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.0", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  
  dt[, c("first","stratify_group") := tstrsplit(variable,".",fixed=T)]
  dt[, stratify_group              := as.character(stratify_group)]
  dt[, coefficient                 := str_extract(first,'([a-z\\_]+)')]
  dt[, event_time                  := str_extract(first,'([-0-9]+)')]
  dt[, event_time                  := as.numeric(event_time)]
  dt[, conf_upb                    := Estimate + 1.96*`Std. Error`]
  dt[, conf_lwb                    := Estimate - 1.96*`Std. Error`]
  
  dt <- dt[event_time %in% c(left_bound:right_bound)]
  
  figure <- dt[str_sub(variable,1,1)=="t"] %>%
    ggplot()+
    geom_vline(xintercept = -1.5, linetype = "dotted", color = "gray30")+
    geom_hline(yintercept=baseline, color = 'black')+
    geom_line(aes( x = event_time, y = Estimate, color = stratify_group), show.legend = TRUE)+
    geom_point(aes( x = event_time, y = Estimate, color = stratify_group,
                    shape = stratify_group), size = 2)+
    geom_errorbar(
      aes(x=event_time,
          ymin = conf_lwb,
          ymax = conf_upb,
          color = stratify_group),
      linetype = "dashed",
      width=0.1)+
    theme_classic()+
    scale_colour_manual(values = c("#A6D854", 'purple'))+
    scale_shape_manual(values=c(15,16),
                       name = stratify_title,
                       labels = stratify_group)+ 
    theme(plot.title = element_text(hjust=0.5),
          plot.subtitle = element_text(hjust=0.5),
          plot.caption = element_text(hjust=0),
          legend.position = "bottom")+
    scale_y_continuous(name=ylabel, limits=yscale, breaks = ybks)+
    scale_x_continuous(name=xlabel, breaks = xbks)+
    labs(title = main_title,
         subtitle = subtitle)+
    guides(color = FALSE, 
           shape = guide_legend(override.aes = list(color = c("#A6D854", 'purple'),
                                                    shape = c(15,16))))  
  return(figure)
}


prep_double <- function(dd){
  
  dt <- copy(dd)
  
  dt[, conf_upb               := att_r + 1.96*`std`]
  dt[, conf_lwb               := att_r - 1.96*`std`]
  
  dt[, reg := 1]
  dt  <- dt[, .(event_time=r,att=att_r,conf_upb,conf_lwb,reg)]
  
}


prep_compare_b1 <- function(dd){
  
  dt <- copy(dd)
  
  dt[, c("first","stratify_group") := tstrsplit(variable,".",fixed=T)]
  dt[, coefficient                 := str_extract(first,'([a-z\\_]+)')]
  dt[, event_time                  := str_extract(first,'([-0-9]+)')]
  dt[, event_time                  := as.numeric(event_time)]
  dt[, conf_upb                    := Estimate + 1.96*`Std. Error`]
  dt[, conf_lwb                    := Estimate - 1.96*`Std. Error`]
  
  dt       <- dt[event_time %in% c(left_bound:right_bound)][order(event_time)]
  dt       <- dt[, .(event_time,att=Estimate,conf_upb,conf_lwb)]
  dt[, reg := 0]
  
  dt       <- add_row(dt, event_time = -1, att = 0, conf_upb = 0, conf_lwb = 0, reg=0)
  
  
  return(dt)
  
}



graph_month <- function(dt){
  
  dt <- add_row(dt, variable = "treated_event_time_stratify-2.1", model = 5, Estimate = 0, "Std. Error" = 0, "t value" = 0, "Pr(>|t|)" = 0, obs = 196890)
  
  dt[, c("first","stratify_group") := tstrsplit(variable,".",fixed=T)]
  dt[, stratify_group              := as.character(stratify_group)]
  dt[, coefficient                 := str_extract(first,'([a-z\\_]+)')]
  dt[, event_time                  := str_extract(first,'([-0-9]+)')]
  dt[, event_time                  := as.numeric(event_time)]
  dt[, conf_upb                    := Estimate + 1.96*`Std. Error`]
  dt[, conf_lwb                    := Estimate - 1.96*`Std. Error`]
  
  dt <- dt[event_time %in% c(m_left_bound:m_right_bound)]
  
  figure <- dt[str_sub(variable,1,1)=="t"] %>%
    ggplot()+
    geom_vline(xintercept = -1.5, linetype = "dotted", color = "gray30")+
    geom_hline(yintercept=baseline, color = 'black')+
    geom_line(aes( x = event_time, y = Estimate, color = stratify_group))+
    geom_point(aes( x = event_time, y = Estimate, color = stratify_group))+
    geom_errorbar(
      aes(x=event_time,
          ymin = conf_lwb,
          ymax = conf_upb),
      linetype = "dashed",
      width=0.1,
      color = "black")+
    theme_classic()+
    theme(plot.title = element_text(hjust=0.5),
          plot.subtitle = element_text(hjust=0.5),
          plot.caption = element_text(hjust=0),
          legend.position = "none")+
    scale_y_continuous(name=ylabel, limits=yscale, breaks = ybks)+
    scale_x_continuous(name=xlabel, breaks = m_xbks)+
    labs(title = main_title,
         subtitle = subtitle)
  return(figure)
}















